package com.rm2pt.generator.springboot.LabManage.service;
import java.util.Date;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rm2pt.generator.springboot.LabManage.entity.*;
import com.rm2pt.generator.springboot.LabManage.utils.DaoManage;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.ServiceManage;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import com.rm2pt.generator.springboot.LabManage.redis.CurrentUtils;

@Transactional
@Service
public class JoinTeamService{
	@Autowired CurrentUtils currentUtils;
	@Autowired private DaoManage DM;
		
	@Autowired private ServiceManage serviceManage;

	public Boolean applyJoinTeam(String teamId,String studentId) throws PreconditionException{
		Team team = (Team)GetData(DM.getTeamDao().findById(teamId));
		Student stu = (Student)GetData(DM.getStudentDao().findById(studentId));
		Apply apply = DM.getApplyDao().findByAimIdAndBelongStudentAndApplyType(teamId,studentId,ApplyType.JOINTEAM);
		
		if(StandardOPs.oclIsUndefined(team) == false && StandardOPs.oclIsUndefined(stu) == false && StandardOPs.oclIsUndefined(apply) == true)
		{
			Apply a = new Apply();
			a.setAimId(teamId);
			a.setBelongStudent(studentId);
			a.setApplyType(0);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
			
	// public Boolean applyJoinGroup(String groupId,String studentId) throws PreconditionException
	
	public Boolean exitGroup(String groupId,String studentId) throws PreconditionException{
		StudentInGroup sig = DM.getStudentInGroupDao().findByGroupIdAndStudentId(groupId,studentId);
		
		if(StandardOPs.oclIsUndefined(sig) == false)
		{
			DM.getStudentInGroupDao().delete(sig);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
	public Boolean exitTeam(String teamId,String studentId) throws PreconditionException{
		StudentInTeam sit = DM.getStudentInTeamDao().findByTeamIdAndStudentId(teamId,studentId);
		List<StudentInGroup> sigs = new ArrayList<StudentInGroup>();
		sigs = DM.getStudentInGroupDao().findByStudentId(studentId);
		List<Group> gs = new ArrayList<Group>();
		gs = DM.getGroupDao().findByBelongTeam(teamId);
		boolean git = false;
		HashSet<String> temId = new HashSet<String>();
		for (Group g : gs) {
			temId.add(g.getId());
		}
		for (StudentInGroup sig : sigs) {
			if (temId.contains(sig.getGroupId())) {
				git = true;
			}
		}
		
		if(StandardOPs.oclIsUndefined(sit) == false && git == false)
		{
			DM.getStudentInTeamDao().delete(sit);
			return true;
			
		}else{
		
		 	 throw new PreconditionException();
		
		}
		
	}
				
	public static Object GetData(Optional<?> op) {
		if (op.isPresent())
			return op.get();
		else 
			return null;
	}
}
